package client.interactor;

import javax.media.j3d.TransformGroup;

import com.sun.j3d.utils.picking.PickCanvas;
import com.sun.j3d.utils.picking.PickResult;

import client.controller.Universe3DController;
import client.presentation.Object3DPresentation;

public class TwoDPickerBehavior implements PickerBehavior{
	
	private Interactor interactor;
	private Universe3DController controller;
	private PickCanvas pickCanvas;
	
	public TwoDPickerBehavior(PickCanvas pc, Interactor i, Universe3DController c) {
		pickCanvas = pc; 
		interactor = i;
		controller = c;
		interactor.setInteractiveObject(controller.getSelectedViewpoint());
	}
	
	public void findInteractiveObject(int x, int y) {
		
		pickCanvas.setShapeLocation(x, y);

	    PickResult [] sgPath = pickCanvas.pickAllSorted () ;
		if (sgPath != null) {
			try {
				TransformGroup objectInInteraction = (TransformGroup)sgPath[0].getNode(PickResult.TRANSFORM_GROUP);
				interactor.setInteractiveObject(((Object3DPresentation)objectInInteraction).getController());
			} catch (Exception e) {
				e.printStackTrace () ;
			}
		}
		else {
			interactor.setInteractiveObject(controller.getSelectedViewpoint());
		}
	}
}
